import {addCartData} from "../../api/cart";
export default{ 
    namespaced:true,
    state:{
        cartData:[],//购物车里的菜品数据
        cartCount:0//购物车里菜品的数量
    },
    mutations:{
        //添加购物车数据
        ["ADD_CART_DATA"](state,payload){
            let isSame=false;//是否有相同的菜品
            state.cartCount=0;
            if(state.cartData.length>0){
                //从addCart传递过来的套餐里菜品明细的gid
                let curNewMealItems=(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):""
                for(let i=0;i<state.cartData.length;i++){
                    //购物车里的套餐菜品明细的gid
                    let newMealItems=(state.cartData[i].new_meal_items && state.cartData[i].new_meal_items.length>0)?JSON.stringify(state.cartData[i].new_meal_items):""
                    //如果有相同的菜品
                    if(state.cartData[i].gid==payload.cartData.gid && newMealItems==curNewMealItems){
                        isSame=true;
                        //增加购物车里菜品的数量
                        state.cartData[i].amount=parseInt(state.cartData[i].amount)+1;
                        //如果套餐里存在菜品
                        if(state.cartData[i].meal_items && state.cartData[i].meal_items.length>0){
                            for(let j=0;j<state.cartData[i].meal_items.length;j++){
                                state.cartData[i].meal_items[j].amount=state.cartData[i].amount*state.cartData[i].meal_items[j].dis_amount;
                            }
                        }
                    }
                    //计算购物车里菜品的数量
                    state.cartCount+=parseInt(state.cartData[i].amount);
                }
            }
            if(!isSame){
                state.cartData.unshift(payload.cartData);
                state.cartCount+=parseInt(payload.cartData.amount);
            }
        }
    },
    actions:{   
        //添加购物车
        addCart(conText,payload){
            //给服务端接口购物车的数据
            let data={
                "uid":conText.rootState.user.uid,
                "token":conText.rootState.user.token,
                "platform":1,
                "branch_shop_id":payload.cartData.branch_shop_id,//分店的id
                "table_code":payload.cartData.table_code,//桌号
                "gid":payload.cartData.gid,//菜品和套餐的唯一标识
                "amount":payload.cartData.amount,//菜品的数量
                "is_meal":payload.cartData.is_meal,//是否套餐
                "meal_type":payload.cartData.meal_type,//套餐类型。0：普通套餐，1：自定义套餐
                "meal_items":(payload.cartData.meal_items && payload.cartData.meal_items.length>0)?JSON.stringify(payload.cartData.meal_items):"",//套餐里菜品的明细
                "new_meal_items":(payload.cartData.new_meal_items && payload.cartData.new_meal_items.length>0)?JSON.stringify(payload.cartData.new_meal_items):""//套餐里菜品的gid
            };
            addCartData(data).then(res=>{
                if(res.code==200){
                    conText.commit("ADD_CART_DATA",{cartData:payload.cartData});
                    if(payload.success){
                        payload.success();
                    }
                }
            })
        }
    }
}